package com.cloudbroker.bcs.platform.ltb.middle.extservice.usercenter.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.cloudbroker.bcs.common.configure.ConfigUtil;
import com.cloudbroker.bcs.csvc.lightinvesting.api.service.IFSService;
import com.cloudbroker.bcs.csvc.lightinvesting.api.t2service.IFST2Service;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.ifs.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.IFS.*;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.back.UserEntityVO;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.user.back.UsersQueryVo;
import com.cloudbroker.bcs.platform.ltb.middle.entity.UserEntity;
import com.cloudbroker.bcs.platform.ltb.middle.extservice.usercenter.UserCenterService;
import com.cloudbroker.bcs.platform.ltb.middle.util.HttpUtil;
import com.cloudbroker.bcs.platform.ltb.middle.util.MD5Util;
import com.cloudbroker.bcs.platform.ltb.middle.util.ssl.SSLHttpUtil;
import com.hundsun.jresplus.remoting.impl.annotation.ServiceParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.HttpURLConnection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tianrui on 2016/11/3.
 */
@Service
public class UserCenterServiceImpl implements UserCenterService {

    private static final Logger logger = LoggerFactory.getLogger(UserCenterServiceImpl.class);

//    private static final String USER_CENTER_HTTP_URL=;

    @Resource
    private IFST2Service ifsService;

    public CheckRegisterVo func_check_register(CheckRegisterForm checkRegisterForm){
        return ifsService.func_check_register(checkRegisterForm);
    }

    @Override
    public RegisterVo func_register(RegisterForm registerForm) {
        return ifsService.func_register(registerForm);
    }

    @Override
    public LoginVo func_login(LoginForm loginForm) {
        return ifsService.func_login(loginForm);
    }

    @Override
    public AuthCodeGetVo func_auth_code_get(AuthCodeGetForm authCodeGetForm) {
        return ifsService.func_auth_code_get(authCodeGetForm);
    }

    @Override
    public AuthCodeLoginVo func_auth_code_login(AuthCodeLoginForm authCodeLoginForm) {
        return ifsService.func_auth_code_login(authCodeLoginForm);
    }

    @Override
    public UserUpdateVo func_user_update(UserUpdateForm userUpdateForm) {
        return ifsService.func_user_update(userUpdateForm);
    }

    @Override
    public UserinfoVo func_userinfo(UserinfoForm userinfoForm) {
        return ifsService.func_userinfo(userinfoForm);
    }

    @Override
    public UserTokenCheckVo func_user_token_check(UserTokenCheckForm userTokenCheckForm) {
        return ifsService.func_user_token_check(userTokenCheckForm);
    }

    @Override
    public LogoutVo func_logout(LogoutForm logoutForm) {
        return ifsService.func_logout(logoutForm);
    }

    @Override
    public PasswordResetVo func_password_reset(PasswordResetForm passwordResetForm) {
        return ifsService.func_password_reset(passwordResetForm);
    }


    @Override
    public UserPageQueryVo func_user_page_query( UserPageQueryForm param){
        return ifsService.func_user_page_query(param);
    }

    @Override
    public AdviserSaveVo func_adviser_save(AdviserSaveForm param){
        return ifsService.func_adviser_save(param);
    }

    @Override
    public AdviserUpdateVo func_adviser_update( AdviserUpdateForm param){
        return ifsService.func_adviser_update(param);
    }

    @Override
    public AdviserGetVo func_adviser_get(AdviserGetForm param){
        return ifsService.func_adviser_get(param);
    }

    @Override
    public AdviserPageQueryVo func_adviser_page_query( AdviserPageQueryForm param){
        return ifsService.func_adviser_page_query(param);
    }

    public UserUpdatePasswordVo func_user_update_password( UserUpdatePasswordForm param){
        return ifsService.func_user_update_password(param);
    }

    public UsersQueryVo func_users_query( String user_ids){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("user_ids",user_ids);
        paramMap.put("source","cloudbroker_2");
        paramMap.put("sign", genSign("cloudbroker_2","1D30DB23C7477E693FDA62E6CDA3168D"));
        String url= ConfigUtil.getString("func.usercenter.http.url")+"/func_users_query";
        String response= SSLHttpUtil.doPostJson(url, JSON.toJSONString(paramMap));
       UsersQueryVo usersQueryVo = JSON.parseObject(response,UsersQueryVo.class);
        if(usersQueryVo==null){
            usersQueryVo = new UsersQueryVo();
        }
       if(usersQueryVo.getData_list()==null){
           usersQueryVo.setData_list(Collections.EMPTY_LIST);
       }
        return usersQueryVo;
    }

    public static String genSign(String source,String signKey){
        Map<String, String> map=new HashMap<>();
        map.put("source", source);
        map.put("signKey", signKey);
        return  MD5Util.encrypt(map.toString());
    }

    public CapitalAccountLoginVo func_capital_account_login( CapitalAccountLoginForm param){
        return ifsService.func_capital_account_login(param);
    }

}
